<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>文章列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/Application/iframe/assets/libs/layuiadmin/layui/css/layui.css"/>
    <link rel="stylesheet" href="/Application/iframe/assets/libs/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="/Application/iframe/assets/libs/layuiadmin/style/mylay.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;width: 100%;">
                <button class="layui-btn layui-btn-danger btn-top" data-type="batchdel">批量删除</button>
                <a class="layui-btn btn-top" href="{:U('create')}">添加文章</a>
                <div class="layui-form search_ctr">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <input type="text" name="keys" placeholder="输入文章名称查询" autocomplete="off" class="layui-input">
                            </div>
                            <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-search">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                </div>
            </div>

            <table id="LAY-table" lay-filter="LAY-table"></table>

            <script type="text/html" id="show">
                {{#  if(d.is_show == 1){ }}
                <span class="yes_span"><i class="layui-icon layui-icon-ok-circle"></i> 是</span>
                {{#  } else { }}
                <span class="no_span"><i class="layui-icon layui-icon-tips"></i> 否</span>
                {{#  } }}
            </script>
            <script type="text/html" id="table-webuser">
                <a class="layui-btn layui-btn-normal layui-btn-xs" href="/admin/announcement/edit/id/{{d.id}}.html"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script type="text/javascript" src="/Application/iframe/assets/libs/layuiadmin/layui/layui.js"></script>
<script>
    var url = '/admin/Announcement';
    layui.config({
        base: '/Application/iframe/assets/libs/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'form', 'layedit'], function(){
        var $ = layui.$
            ,form = layui.form
            ,table = layui.table
            ,layedit = layui.layedit;

        //初始化列表
        table.render({
            elem: '#LAY-table'
            ,url: url + "/getList.html?v=" + new Date().getTime() //接口
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'id', width: 80, title: 'ID', sort: true}
                ,{field: 'title', minWidth:220, title: '标题'}
                ,{field: 'is_show', width: 90, title: '发布', templet: '#show', align:'center', sort: true}
                ,{field: 'add_date', width: 180, title: '时间'}
                ,{title: '操作', width: 150, align:'center', fixed: 'right', toolbar: '#table-webuser'}
            ]]
            ,page: true
            ,limit: 20
            ,height: 'full-100'
            ,text: {none: '暂无相关数据'}
        });

        //监听搜索
        form.on('submit(LAY-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('LAY-table', {
                url: url + '/getList.html'
                ,where: field
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

        //监听编辑+单个删除
        table.on('tool(LAY-table)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        type:"post",
                        url:url + "/delete.html",
                        data: {id:data.id},
                        dataType:"json",
                        beforeSend: function () {
                            layer.load(1);
                        },
                        success:function(res){
                            layer.closeAll();
                            if(res.code == 1001){
                                obj.del();
                            }
                            else {
                                layer.msg(res.msg);
                            }
                        }
                    });

                });
            }
        });

        //批量删除+添加
        var active = {
            batchdel: function(){
                var checkStatus = table.checkStatus('LAY-table')
                    ,checkData = checkStatus.data //得到选中的数据
                    ,ids = "0";
                if(checkData.length === 0){
                    return layer.msg('请选择数据');
                }
                $.each(checkData,function(key,value){
                    ids = ids + "," + checkData[key]['id'];
                });
                layer.confirm('确定删除吗？', function(index) {
                    $.ajax({
                        type:"post",
                        url:url + "/delete.html",
                        data: {id:ids},
                        dataType:"json",
                        beforeSend: function () {
                            layer.load(1);
                        },
                        success:function(res){
                            layer.closeAll();
                            if(res.code == 1001){
                                table.reload('LAY-table',{where: {time:new Date()}});
                                layer.msg('已删除');
                            }
                            else {
                                layer.msg(res.msg);
                            }
                        }
                    });
                });
            }
        };

        $('.layui-btn.btn-top').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>